home *** CD-ROM | disk | FTP | other *** search
/ Online Today 2000 January / Onto0100.iso / mac / MACAST 1.0 / MACAST Documentation / Plugin Development / SimpleVP / Source / main.cp next >
Encoding:
Text File  |  1999-09-16  |  5.8 KB  |  282 lines  |  [TEXT/CWIE]

  1. /*
  2.     White Lines
  3.     ⌐1999, @soft
  4.  
  5.     Description:    Simple and NOT COMMENTED visual plugin for MACAST 1.0.
  6.     Version:        1.2
  7.     Released:        7/11/99
  8.     Compatibility:    MACAST 1.0
  9.     Version history:
  10.  
  11.      Date    Who        Changes
  12.     ------+------+------------------------------------------------------
  13.     071199    SKA        Modified to be used with Visual Plugin API 1.2
  14.     050299    SKA        Simple plugin is done.
  15. */
  16.  
  17. #include "MACAST_Visual.h"
  18.  
  19. const OSType    kAuthorID    = 'exmp';    // Please change the author code to different value to avoid collisions.
  20. const OSType    kPluginID    = 'RGBs';
  21.  
  22. VPInfoBlock gPlugInfo =
  23. {
  24.     VP_INFOBLOCK_HEADER(kAuthorID, kPluginID),
  25.  
  26.     "\pRGBSpectrum x2",
  27.  
  28.     VisInitialize,
  29.     VisTerminate,
  30.     nil,                // we don't idle
  31.     VisAbout,
  32.     VisDraw,
  33.     VisClick,
  34.     
  35.     nil,                // we don't want keydowns..
  36.     nil,                // ...and events
  37.     
  38.     VisError,
  39.     nil,                // we have no settings
  40.     VisTrackBegin,
  41.     VisTrackEnd,
  42.     nil,                // no listen proc
  43.     
  44.     // Nifty macro to set all these 'reserved' values for us
  45.     VP_INFOBLOCK_FOOTER
  46. };
  47.  
  48. typedef struct
  49. {
  50.     short        num;
  51.     RGBColor    color[];
  52. } Colors, *ColorsPtr, **ColorsHandle;
  53.  
  54. typedef struct
  55. {
  56.     UInt16    colorIndex;
  57. } OwnPrefs, *OwnPrefsPtr;
  58.  
  59. ColorsHandle    gColors;
  60. RGBColor        gCurrColor;
  61. UInt16            gResFork;
  62. UInt16            gValues[300];
  63. UInt8*            gFFTArray;
  64. Rect            gRect;
  65. GWorldPtr         gWorld        = nil;
  66. OwnPrefs        gPrefs;
  67. UInt32            gAboutExpire = nil;
  68. Boolean            gAbout = false;
  69. QDGlobalsPtr    gQD;
  70.  
  71. OSStatus    VisInitialize(FSSpecPtr inPlugin, WindowPtr* outWindow, UInt32* ioRefcon)
  72. {
  73.     OSStatus    err;
  74.     
  75.     gResFork = FSpOpenResFile(inPlugin, fsRdPerm);
  76.     gQD = gPlugInfo.ma->GetQDGlobals();
  77.     
  78.     // Get list of colors
  79.     gColors = (ColorsHandle)GetResource('COLR', 128);
  80.     if (!gColors)
  81.         return errVisTerminate;
  82.         
  83.     DetachResource((Handle)gColors);
  84.  
  85.     // Load own preferences
  86.     UInt16    size = sizeof(OwnPrefs);
  87.     err = gPlugInfo.ma->ReadPrefs(kAuthorID, kPluginID, (Ptr)&gPrefs, &size);
  88.     if (err != noErr || size != sizeof(OwnPrefs))    // Preferences can't be read, we need to make a struct and fill
  89.                         // it with default values
  90.     {
  91.         gPrefs.colorIndex = nil;
  92.         
  93.         // Save the preferences right away
  94.         gPlugInfo.ma->SavePrefs(kAuthorID, kPluginID, (Ptr)&gPrefs, sizeof(OwnPrefs));
  95.     }
  96.     
  97.     HLock((Handle)gColors);
  98.     if (gPrefs.colorIndex >= (*gColors)->num)
  99.         gPrefs.colorIndex = nil;
  100.     gCurrColor = (*gColors)->color[gPrefs.colorIndex];
  101.     HUnlock((Handle)gColors);
  102.     
  103.     // Aquire FFT array
  104.     gPlugInfo.ma->GetValues(&gFFTArray, &size);
  105.     
  106.     // Create work GWorld
  107.     CGrafPtr    savePort;
  108.     GDHandle    saveDevice;
  109.  
  110.     SetRect(&gRect, 0, 0, 300, 50);
  111.     err = ::NewGWorld(&gWorld, nil, &gRect, nil, nil, nil);
  112.  
  113.     GetGWorld(&savePort, &saveDevice);
  114.     SetGWorld(gWorld, nil);
  115.     LockPixels(::GetGWorldPixMap(gWorld));
  116.     ForeColor(whiteColor);
  117.     BackColor(blackColor);
  118.     TextFont(21);
  119.     TextSize(12);
  120.     TextFace(bold);
  121.     EraseRect(&gRect);
  122.     UnlockPixels(::GetGWorldPixMap(gWorld));
  123.     SetGWorld(savePort, saveDevice);
  124.  
  125.     for (short i=0;i<300;i++)
  126.         gValues[i] = 0;
  127.     
  128.     
  129.     // Create own window
  130.     *outWindow = NewWindow(nil, &gRect, "\pRGBSpectrum", false, 1985, (WindowPtr)-1, true, nil);
  131.     if (*outWindow == nil)
  132.         return errVisTerminate;
  133.  
  134.     MoveWindow(*outWindow, gQD->screenBits.bounds.right - 320, gQD->screenBits.bounds.bottom - 80, true);
  135.     ShowWindow(*outWindow);
  136.     
  137.     return errVisNoErr;
  138. }
  139.  
  140. OSStatus    VisTerminate(WindowPtr inWindow, UInt32* ioRefcon)
  141. {
  142.     // Save preferences
  143.     gPlugInfo.ma->SavePrefs(kAuthorID, kPluginID, (Ptr)&gPrefs, sizeof(OwnPrefs));
  144.  
  145.     DisposeWindow(inWindow);
  146.     
  147.     // Dispose everything we allocated
  148.     if (gWorld)
  149.         DisposeGWorld(gWorld);
  150.         
  151.     if (gColors)
  152.         DisposeHandle((Handle)gColors);
  153.         
  154.     if (gResFork != -1)
  155.         CloseResFile(gResFork);
  156.  
  157.     return errVisNoErr;
  158. }
  159.  
  160. OSStatus    VisAbout(WindowPtr inWindow, UInt32* ioRefcon)
  161. {
  162.     gAbout = true;
  163.     gAboutExpire = TickCount() + 360;
  164.     return errVisNoErr;
  165. }
  166.  
  167. OSStatus VisDraw(WindowPtr inWindow, UInt32* ioRefcon)
  168. {
  169.     // Calculate values
  170.     UInt32 timer, status;
  171.     gPlugInfo.ma->GetStatus(&timer, &status);
  172.     
  173.     if (status & statusStopped)
  174.     {
  175.         for (short i=0;i<300;i++)
  176.             gValues[i] = 0;    
  177.     }
  178.     else
  179.     {
  180.         for (short i=0, a=4; i<300; i++, a++)
  181.         {
  182.             gValues[i] = gFFTArray[a]/2;
  183.             
  184.             if (gValues[i] > 48)
  185.                 gValues[i] = 48;
  186.         }
  187.     }
  188.  
  189.     // Render offscreen
  190.     CGrafPtr    savePort;
  191.     GDHandle    saveDevice;
  192.  
  193.     GetGWorld(&savePort, &saveDevice);
  194.     SetGWorld(gWorld, nil);
  195.     LockPixels(::GetGWorldPixMap(gWorld));
  196.     BackColor(blackColor);
  197.     EraseRect(&gRect);
  198.     
  199.     // If we have about active, draw it
  200.     if (gAbout)
  201.     {
  202.         if (TickCount() < gAboutExpire)
  203.         {
  204.             ForeColor(whiteColor);
  205.             MoveTo(150, 25);
  206.             DrawString("\pRGBSpectrum");
  207.             MoveTo(200, 38);
  208.             DrawString("\pby slava");
  209.         }
  210.         else
  211.             gAbout = false;
  212.     }
  213.     
  214.     RGBForeColor(&gCurrColor);
  215.     MoveTo(0,50);
  216.     for (short i=0;i<300;i++)
  217.     {
  218.         Line(0, -gValues[i]);
  219.         Move(1, gValues[i]);
  220.     }
  221.     
  222.     // Nice decoration time!
  223.     RGBColor    decor = { 50000, 50000, 50000 };
  224.     MoveTo(0, 50);
  225.     PenMode(adMin);
  226.     PenSize(1,2);
  227.     for (short i=0;i<50;i+=2)
  228.     {
  229.         RGBForeColor(&decor);
  230.         MoveTo(0,50-i);
  231.         Line(320,0);
  232.         
  233.         decor.red -= 2000;
  234.         decor.green -= 2000;
  235.         decor.blue -= 2000;
  236.     }
  237.     PenNormal();
  238.     
  239.     SetGWorld(savePort, saveDevice);
  240.     
  241.     // Copy to our window
  242.     GrafPtr    oldPort;
  243.     GetPort(&oldPort);
  244.     SetPort(inWindow);
  245.     ::CopyBits(&((GrafPtr)gWorld)->portBits, &inWindow->portBits,
  246.             &gRect, 
  247.             &gRect, 
  248.             srcCopy, nil);
  249.     UnlockPixels(::GetGWorldPixMap(gWorld));
  250.     SetPort(oldPort);
  251.     return errVisNoErr;
  252. }
  253.  
  254. OSStatus VisClick(WindowPtr inWindow, Point inClick, UInt32* ioRefcon)
  255. {
  256.     // MACAST will handle window drags and goAway clicks for us.
  257.     HLock((Handle)gColors);
  258.     gPrefs.colorIndex++;
  259.     if (gPrefs.colorIndex >= (*gColors)->num)
  260.         gPrefs.colorIndex = nil;
  261.     gCurrColor = (*gColors)->color[gPrefs.colorIndex];
  262.     HUnlock((Handle)gColors);
  263.     
  264.     return errVisNoErr;
  265. }
  266.  
  267. Boolean VisError(StringPtr outErrString, OSStatus* outErrNum)
  268. {
  269.     return false;
  270. }
  271.  
  272. OSStatus VisTrackBegin(WindowPtr inWindow, UInt32* ioRefcon)
  273. {
  274.     return errVisNoErr;
  275. }
  276.  
  277. OSStatus VisTrackEnd(WindowPtr inWindow, UInt32* ioRefcon)
  278. {
  279.     return errVisNoErr;
  280. }
  281.  
  282.